<template>
	<view>
		<view class="form">
			<u-form-item label="店铺名称" label-width="150rpx">
				<u-input v-model="form.name" type="text" placeholder="请输入" />
			</u-form-item>
			<u-form-item label="店铺分类" label-width="150rpx">
				<u-input v-model="catename" @click="cateShow = true" type="select" placeholder="请选择" />
			</u-form-item>
			<u-form-item label="封面图" label-width="150rpx">
				<u-upload width="160" height="160" :action="action" max-count="1" :file-list="coverlist"
					@on-success="coverSuccess" @on-remove="coverRemove"></u-upload>
			</u-form-item>

			<u-form-item label="店铺描述" label-width="200rpx" label-position="top">
				<u-input v-model="form.remark" type="textarea" height="200" :maxlength="-1" placeholder="请输入店铺描述" />
			</u-form-item>
			<u-form-item label="联系人" label-width="150rpx">
				<u-input v-model="form.contactName" placeholder="请输入" />
			</u-form-item>
			<u-form-item label="联系电话" label-width="150rpx">
				<u-input v-model="form.contactPhone" placeholder="请输入" />
			</u-form-item>
		</view>
		<u-picker mode="time" v-model="stimeShow" :params="params" @confirm="stimeConfirm"></u-picker>
		<u-picker mode="time" v-model="etimeShow" :params="params" @confirm="etimeConfirm"></u-picker>
		<u-select v-model="cateShow" label-name="name" value-name="id" mode="mutil-column-auto" :list="catelist"
			@confirm="cateConfirm"></u-select>
		<view style="height: 120rpx;"></view>
		<botbtn btntext="提交资料" @confirem="submit"></botbtn>
	</view>
</template>

<script>
	import {
		API_URL
	} from '@/env.js'
	import botbtn from '@/components/Botbtn/Botbtn.vue'
	export default {
		components: {
			botbtn,
		},
		data() {
			return {
				type: 'add',
				form: {
					id: '',
					name: '',
					classId: '',
					pclassId: '',
					pic: '',
					open_stime: '',
					open_etime: '',
					lin: '',
					lon: '',
					address: '',
					zhizhao: '',
					yingye: '',
					pics: '',
					remark: '',
					contactName: '',
					contactPhone: '',
				},
				coverlist: [],
				imglist: [],
				cardlist: [],
				action: API_URL + '/tc/uploadToMinio',
				list: [{
					label: '周一',
					name: '1',
					checked: false,
				}, {
					label: '周二',
					name: '2',
					checked: false,
				}, {
					label: '周三',
					name: '3',
					checked: false,
				}, {
					label: '周四',
					name: '4',
					checked: false,
				}, {
					label: '周五',
					name: '5',
					checked: false,
				}, {
					label: '周六',
					name: '6',
					checked: false,
				}, {
					label: '周日',
					name: '7',
					checked: false,
				}],
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
				stimeShow: false,
				etimeShow: false,
				cateShow: false,
				catename: '',
				catelist: []
			};
		},
		onLoad(e) {
			uni.showLoading({
				title: '加载中'
			})
			this.$u.get('/home/job/getNav',{type:'find'}).then(res => {
			console.log(res)
				this.catelist = res;
			})
			this.$u.get('/my/userStore').then(res => {

				uni.hideLoading();
				if (res && res.id) {
					this.type = 'edit';
					this.form = {
						id: res.id,
						name: res.name,
						classId: res.category.id,
						pclassId: res.pcategory.id,
						pics: res.pics,
						open_stime: res.yingyeTime.split('-')[0],
						open_etime: res.yingyeTime.split('-')[1],
						lin: res.lin,
						lon: res.lon,
						address: res.address,
						zhizhao: res.zhizhao,
						yingye: res.yingye,
						pics: res.pics,
						remark: res.remark,
						contactName: res.contactName,
						contactPhone: res.contactPhone,
					}
					this.catename = res.pcategory.name + '/' + res.category.name;
					this.coverlist = res.coverList;
					this.cardlist = res.cardList;
					this.imglist = res.picList;
					this.list.map((item, index) => {
						if (res.yingye.search(item.name) != -1) {
							this.list[index]['checked'] = true;
						}
					})
				} else {
					this.type = 'add';
				}
			})
		},
		methods: {
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				let str = e.join(',');
				this.form.yingye = str;
			},
			opensite() {
				var that = this;
				uni.chooseLocation({
					success: function(res) {
						that.form.lon = res.latitude;
						that.form.lin = res.longitude;
						that.form.address = res.address;
					}
				});
			},
			stimeConfirm(e) {
				this.form.open_stime = e.hour + ':' + e.minute;
			},
			etimeConfirm(e) {
				this.form.open_etime = e.hour + ':' + e.minute;
			},
			cateConfirm(e) {
				this.form.pclassId = e[0].value;
				this.form.classId = e[1].value;
				this.catename = e[0].label + '/' + e[1].label;
			},
			imgRemove(index, lists, name) {
				let banner = [];
				lists.map((item) => {
					banner.push(item.response.data);
				})
				if (banner.length > 0) {
					this.form.pics = banner.join(',');
				} else {
					this.form.pics = '';
				}
			},
			imgSuccess(data, index, lists, name) {
				let banner = [];
				lists.map((item) => {
					banner.push(item.response.data);
				})
				if (banner.length > 0) {
					this.form.pics = banner.join(',');
				}
			},
			coverRemove(index, lists, name) {
				let banner = [];
				lists.map((item) => {
					banner.push(item.response.data);
				})
				if (banner.length > 0) {
					this.form.pic = banner.join(',');
				} else {
					this.form.pic = '';
				}
			},
			coverSuccess(data, index, lists, name) {
				let banner = [];
				lists.map((item) => {
					banner.push(item.response.data);
				})
				if (banner.length > 0) {
					this.form.pic = banner.join(',');
				}
			},
			cardRemove(index, lists, name) {
				let banner = [];
				lists.map((item) => {
					banner.push(item.response.data);
				})
				if (banner.length > 0) {
					this.form.zhizhao = banner.join(',');
				} else {
					this.form.zhizhao = '';
				}
			},
			cardSuccess(data, index, lists, name) {
				let banner = [];
				lists.map((item) => {
					banner.push(item.response.data);
				})
				if (banner.length > 0) {
					this.form.zhizhao = banner.join(',');
				}
			},
			submit() {
				if (this.form.name == '') {
					return this.$u.toast('请输入店铺名称');
				}

				uni.showLoading({
					title: '提交中'
				})
				this.$u.post('/tc/saveStore', this.form).then(res => {
					uni.hideLoading();
					this.$u.toast('提交成功');
					var that = this;
					if (this.type == 'add') {
						setTimeout(() => {
							that.$u.route({
								type: 'redirectTo',
								url: '/pagesFind/store_pay/store_pay',
								params: {
									id: res
								}
							});
						}, 1500)
					} else {
						setTimeout(() => {
							uni.navigateBack({

							})
						}, 1500)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.form {
		padding: 20rpx;
		background-color: white;

		.openbox {
			display: flex;
			flex-direction: column;

			.opentime {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 20rpx;
			}
		}
	}
</style>
